// const IMG_URL = "http://yyh.vipgz1.idcfengye.com/chuyujing"
// const BASE_URL = "http://yyh.vipgz1.idcfengye.com/chuyujing"

const IMG_URL = "https://pyzl.51yunmai.com"
const BASE_URL = "https://pyzl.51yunmai.com"
const BASE_URL2 = "https://nmssafety.51yunmai.com"
class Request {
	/**
	 * Request请求方法
	 * @param  {String} url    链接
	 * @param  {Objece} params 参数
	 * @param  {Boolean} isToken  是否携带token
	 * @return {Promise}       包含抓取任务的Promise
	 */
	get(url, isToken) {

		if (isToken === undefined) isToken = true;

		let sessionId = uni.getStorageSync('sessionId') || '';

		const promise = new Promise((resolve, reject) => {
			uni.request({
				url: `${BASE_URL2}${url}`,
				method: 'GET',
				header: {
					'Content-Type': 'application/x-www-form-urlencoded'
				},
				success: res => {
					if (res.statusCode == 200) {
						if (res.data.code == 200 || res.data.code == 3) {
							resolve(res);
						} else if (res.data.code == 500) {
							uni.showToast({
								title: res.data.Message,
								icon: 'none',
								duration: 2000
							})
							uni.clearStorageSync()
							console.log("触发清理")
							// uni.reLaunch({
							// 	url: "/pages/login/login"
							// })
						} else {
							uni.showToast({
								title: res.data.Message,
								icon: 'none',
								duration: 2000
							})
							reject();
						}
					} else {
						uni.showToast({
							title: '[' + res.code + '] 服务器出错,请重试',
							icon: 'none',
							duration: 800
						});
						reject();
					}
				},
				fail: err => {
					uni.showToast({
						title: '网络错误',
						icon: 'none',
						duration: 800
					});
					reject();
				},
				complete: res => {

				}
			});
		});

		if (token == '' && isToken) {
			return new Promise((resolve, reject) => {
				// uni.navigateTo({
				// 	url: '/pages/login/login',
				// })
			});
		} else {
			return promise;
		}
	}

	post(url, params, isToken) {
		if (isToken === undefined) isToken = true;
		if (params === undefined) params = {};
		let token = uni.getStorageSync('token') || '';
		params.token = token
		const promise = new Promise((resolve, reject) => {
			uni.request({
				url: `${BASE_URL2}${url}`,
				method: 'POST',
				data: params, //置入token
				header: {
					'Content-Type': 'application/x-www-form-urlencoded'
				},
				success: res => {
					if (res.statusCode == 200) {
						if (res.data.Code == 200) {
							resolve(res);
						} else if (res.data.Code == 500) {
							uni.showToast({
								title: res.data.Message,
								icon: 'none',
								duration: 2000
							})
							// uni.clearStorageSync()
							// console.log("触发清理")
							// uni.reLaunch({
							// 	url:"/pages/login/login"
							// })
						} else {
							uni.showToast({
								title: res.data.Message,
								icon: 'none',
								duration: 2000
							})
							reject();
						}
					} else {
						uni.showToast({
							title: '[' + res.code + '] 服务器出错,请重试',
							icon: 'none',
							duration: 800
						});
						reject();
					}
				},
				fail: err => {
					uni.showToast({
						title: '网络错误',
						icon: 'none',
						duration: 800
					});
					reject();
				},
				complete: res => {

				}
			});
		});

		if (token == '' && isToken) {
			return new Promise((resolve, reject) => {
				// uni.navigateTo({
				// 	url: '/pages/login/login',
				// })
			});
		} else {
			return promise;
		}
	}

	post2(url, params, isToken) {
		if (isToken === undefined) isToken = true;
		if (params === undefined) params = {};
		let token = uni.getStorageSync('token') || '';
		if (isToken == true) {
			params.token = token
		}
		const promise = new Promise((resolve, reject) => {
			uni.request({
				url: `${BASE_URL}${url}`,
				method: 'POST',
				data: params, //置入token
				header: {
					'Content-Type': 'application/x-www-form-urlencoded'
				},
				success: res => {
					uni.hideLoading()
					// if (res.statusCode == 200) {
					if (res.data.Code == 200) {
						resolve(res);
					} else if (res.data.Code == 500) {
						if(url!='/Account/LoginByAPPOpenid'){
							uni.showToast({
								title: res.data.Message,
								icon: 'none',
								duration: 2000
							})
						}else{
							uni.showToast({
								title: '登录失效',
								icon: 'none',
								duration: 2000
							})
						}
						uni.clearStorageSync()
						console.log("触发清理")
					} else {
						uni.showToast({
							title: res.data.Message,
							icon: 'none',
							duration: 2000
						})
						reject();
					}
				},
				fail: err => {
					uni.showToast({
						title: '网络错误',
						icon: 'none',
						duration: 800
					});
					reject();
				},
				complete: res => {

				}
			});
		});

		if (token == '' && isToken) {
			return new Promise((resolve, reject) => {
				
			});
		} else {
			return promise;
		}
	}

	post3(url, params, isToken) {
		if (isToken === undefined) isToken = true;
		if (params === undefined) params = {};
		let token = uni.getStorageSync('token') || '';
		if (isToken == true) {
			params.token = token
		}
		const promise = new Promise((resolve, reject) => {
			uni.request({
				url: `${BASE_URL}${url}`,
				method: 'GET',
				data: params, //置入token
				header: {
					'Content-Type': 'application/x-www-form-urlencoded'
				},
				success: res => {
					// if (res.statusCode == 200) {
					if (res.data.Success == true) {
						resolve(res);
					} else {
						uni.hideLoading()
						uni.showToast({
							title: res.data.Message,
							icon: 'none',
							duration: 2000
						})
						reject();
					}
				},
				fail: err => {
					uni.hideLoading()
					uni.showToast({
						title: '网络错误',
						icon: 'none',
						duration: 800
					});
					reject();
				},
				complete: res => {

				}
			});
		});

		if (token == '' && isToken) {
			return new Promise((resolve, reject) => {
				// uni.navigateTo({
				// 	url: '/pages/login/login',
				// })
			});
		} else {
			return promise;
		}
	}

}

let request = new Request();

export default request;

// module.exports = {
//   get: request.getApi,
//   post: request.postApi,
//   url: BASE_URL,
//   imgurl: IMG_URL
// }
